@extends('admin.layout')
@section('content')
<div class="admin-biaogelist">
    <div class="listbiaoti am-cf">
      <ul class="am-icon-flag on">用户列表</ul>
      <dl class="am-icon-home" style="float: right;"> 当前位置： 首页 > <a href="javascript:void(0);">用户列表</a></dl>
    </div>
	<div class="am-btn-toolbars am-btn-toolbar am-kg am-cf">
    <form action="/dashboard/list_user" method="get" id="search_form">
        <ul class="tabBarBtn">
            <li style="float:right;margin-right:10px"><input type="button" id="sync" class="am-btn am-radius am-btn-xs am-btn-success" style=""  {{--data-am-modal="{target: '#my-modal-loading'}"--}} value="同步" /></li>
            <li style="float:right"><input type="button" id="to_be_open" class="am-btn am-radius am-btn-xs am-btn-success" style="" value="待启用" /></li>
            <li style="float:right"><input type="button" id="to_be_deleted" class="am-btn am-radius am-btn-xs am-btn-danger" style="" value="待删除" /></li>
            <li><input type="text" class="am-form-field am-input-sm am-input-xm" name="name" value="{{$_GET['name'] or ''}}" placeholder="用户名" style="height:30px;line-height:30px;width:200px" /></li>
            <li><input type="text" class="am-form-field am-input-sm am-input-xm" name="phone" value="{{$_GET['phone'] or ''}}" placeholder="手机号" style="height:30px;line-height:30px;width:200px" /></li>
            <li><input type="submit" class="am-btn am-radius am-btn-xs am-btn-success" style="" value="搜索" /></li>
            <input type="hidden" name="status_type" id="status_type" value="{{$_GET['status_type'] or ''}}"/>
        </ul>
    </form>
</div>

    <div class="am-form am-g">
          <table width="100%" class="am-table am-table-bordered am-table-radius am-table-striped">
            <thead>
              <tr class="am-success">
                <th class="table-id">ID</th>
                <th class="table-title">user_id</th>
                <th class="table-title">用户名</th>
                <th class="table-title">手机号</th>
                {{--<th class="table-title">职务</th>--}}
                <th class="table-type">存储空间(MB)</th>
                <th class="table-date am-hide-sm-only">已用空间(MB)</th>
                <th class="table-title">账户状态</th>
                <th class="table-title">同步状态</th>
                <th width="163px" class="table-set">操作</th>
              </tr>
            </thead>
            <tbody>
            @foreach ($users as $user)
                <tr>
                    <td>{{($loop->index)+1}}</td>
                    <td>{{$user->qyh_userid}}</td>
                    <td>{{$user->nickname}}</td>
                    <td>{{$user->phone or ''}}</td>
{{--                    <td>{{$user->role == '0' ? "学生" : ($user->role == '1' ? "教师" : "其他")}}</td>--}}
                    <td>{{round($user->total_storage/1024, 1)}}</td>
                    <td class="am-hide-sm-only">{{round($user->used_storage/1024, 2)}}</td>
                    <td>{{$user->status == '1' ? "开启" : "关闭"}}</td>
                    <td>{{$user->synced_with_qyh == '1' ? "已同步" : "未同步"}}</td>
                    <td><div class="am-btn-toolbar">
                            <div class="am-btn-group am-btn-group-xs">
                                @if(isset($_GET['status_type']) && $_GET['status_type'] == '1')
                                    <button class="am-btn am-btn-default am-btn-xs am-text-secondary am-round enable_user" data-id="{{$user->id}}" data-total_storage="{{$user->total_storage}}">启用</button>
                                @elseif(isset($_GET['status_type']) && $_GET['status_type'] == '2')
                                    @if($user->status == '1')
                                        <button class="am-btn am-btn-default am-btn-xs am-text-danger am-round disable_user" data-id="{{$user->id}}">停用</button>
                                    @else
                                        <button class="am-btn am-btn-default am-btn-xs am-text-danger am-round del_user" data-id="{{$user->id}}">删除</button>
                                    @endif
                                @else
                                    <button class="am-btn am-btn-default am-btn-xs am-text-secondary am-round modify_user" data-id="{{$user->id}}">编辑{{--<span class="am-icon-pencil-square-o"></span>--}}</button>
                                @endif
                            </div>
                        </div></td>
                </tr>
            @endforeach
            </tbody>
          </table>
{{--        {{isset($_GET['name']) ? $users->appends(['name' => $_GET['name']])->links() : $users->links()}}--}}
        {!! $users->render() !!}
          <hr />
          {{--<p>注：.....</p>--}}
        </div>
</div>
@stop
@section('loading')
<a  class="am-btn am-btn-success" id="loading" data-am-modal="{target: '#my-modal-loading'}">Modal Loading</a>

<div class="am-modal am-modal-loading am-modal-no-btn" tabindex="-1" id="my-modal-loading">
    <div class="am-modal-dialog">
        <div class="am-modal-hd" id="loading_info">正在载入...</div>
        <div class="am-modal-bd">
            <span class="am-icon-spinner am-icon-spin"></span>
        </div>
    </div>
</div>
@stop
@section('ownJs')
    <script>
        var department_id_list,department_index = 0,keys = [];
        $('.modify_user').click(function(){
            var id = $(this).data('id') ;
            location.href = '/dashboard/user/'+id;
        });
        $('.enable_user').click(function(){
            var id = $(this).data('id') ;
            var total_storage = $(this).data('total_storage') ;
            if(parseInt(total_storage) > 0){//先改存储空间再启用
                enable_user(id);
            }else{
                location.href = '/dashboard/user/'+id;
            }
        });
        $('.disable_user').click(function(){
            var id = $(this).data('id') ;
            disable_user(id);
        });
        $('.del_user').click(function(){
            if(confirm("删除操作不可撤销，确认删除?")){
                var id = $(this).data('id') ;
                $.ajax({
                    type: 'get',
                    url: "/dashboard/delete/"+id,
                    dataType: 'json',
//                async:false,
                    success: function (result) {
                        if (result.code == 0) {
                            alert("删除成功!");
                            location.href = location.href;
                        }else if(result.code == -1){
                            location.href = '/dashboard/login';
                        }else{
                            alert("删除失败!");
                            location.href = location.href;
                        }
                    }
                });
            }
        });
        $('#to_be_open').click(function(){
            $('#status_type').val('1');
            $('#search_form').submit();
        });
        $('#to_be_deleted').click(function(){
            $('#status_type').val('2');
            $('#search_form').submit();
        });
        $('#sync').click(function(){
            $('#loading').trigger('click');
            getDepartment_id_list();
        });
        function getDepartment_id_list(){
            $.ajax({
                type: 'get',
                url: "/dashboard/sync/init",
                dataType: 'json',
//                async:false,
                success: function (result) {
                    if (result.code == 0) {
                        department_id_list = result.data;
                        if(Object.keys(department_id_list).length != 0){
                            keys = [];department_index=0;
                            for (var id in department_id_list){
                                keys.push(id);
                            }
                            //同步
                            sync_users(department_id_list[keys[department_index]]);
                        }else{
                            alert("部门为空");
                            $('.am-dimmer').trigger('click');
                        }
                    }else if(result.code == -1){
                        location.href = '/dashboard/login';
                    }
                },
                error:function(a,b,c){
                    alert("获取部门列表失败");
                    $('.am-dimmer').trigger('click');
                }
            });
        }
        function sync_users(id){
            $.ajax({
                type: 'get',
                url: "/dashboard/sync/"+id,
                dataType: 'json',
//                async:false,
                success: function (result) {
                    if (result.code == 0) {
                        //判断是第几个
                        if(department_index == (Object.keys(department_id_list).length-1)){
                            department_index++;
                            $('#loading_info').html("正在同步中,已完成"+(department_index/Object.keys(department_id_list).length * 100).toFixed(2)+'%');
                            $('.am-dimmer').trigger('click');
                            setTimeout(function(){
                                alert("同步成功");
                                location.href = location.href;
                            },1000);
                        }else{
                            department_index++;
                            $('#loading_info').html("正在同步中,已完成"+(department_index/Object.keys(department_id_list).length * 100).toFixed(2)+'%');
                            sync_users(department_id_list[keys[department_index]]);
                        }
                    }else if(result.code == -1){
                        location.href = '/dashboard/login';
                    }
                },
                error:function(a,b,c){
                    alert("同步失败");
                    $('.am-dimmer').trigger('click');
                }
            });

        }
        function enable_user(id){
            $.ajax({
                type: 'get',
                url: "/dashboard/enable_user/"+id,
                dataType: 'json',
                async:false,
                success: function (result) {
                    if (result.code == 0) {
                        location.href = location.href;
                    }else if(result.code == -1){
                        location.href = '/dashboard/login';
                    }else{
                        alert(result.msg);
                    }
                }
            });
        }
        function disable_user(id){
            $.ajax({
                type: 'get',
                url: "/dashboard/disable_user/"+id,
                dataType: 'json',
                async:false,
                success: function (result) {
                    if (result.code == 0) {
                        location.href = location.href;
                    }else if(result.code == -1){
                        location.href = '/dashboard/login';
                    }else{
                        alert(result.msg);
                    }
                }
            });
        }
    </script>
@stop